home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2725 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  53 lines

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reading large files
  5. Date: Tue, 23 Jan 1996 15:10:31 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3104DE47.126F@cmt.lpr.mail.carel.fi>
  8. References: <310411A5.3438@cyber.cyb-live.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=iso-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Alex Ibrado wrote:
  16. > Hello! I have a friend who wants to read in a file of 1,000,000 doubles
  17. > into memory. (She's using Borland C++ 4.0/Windows.) The question is:
  18. > can it be done using a single fread? Since the algo requires reading in
  19. > the values several times (with a different offset each time), I
  20. > suggested using fseek instead of reading the whole file. However, with
  21. > 1,000,000 points read several times, this could be slow. sizeof(size_t)
  22. > returns 2; does this mean she could only read in 65535 doubles at a
  23. > given time?
  24. > Would really appreciate any help you could extend. Thanks!
  25.  
  26. Actually, this newsgroup is not necessarily the right one for this question 
  27. (should try comp.os.msdos.programmer or comp.os.ms-windows.programming.misc, 
  28. depending on which one your program is targeted to), but I'll give you a few 
  29. hints. First, in 16-bit world (DOS, Win3x), size_t is normally a typedef for 
  30. `unsigned int┤, which in fact is 0..65535. Thus: you can only read 65535 
  31. _items_ at a time. However, your item might be something like ten doubles, so 
  32. you'd end up reading 10*65535, ie. 655350 doubles at a time. Second, Intel 
  33. machines in 16-bit mode use _segments_ of upto 65536 bytes, and a 16-bit read 
  34. _cannot_ cross segment boundaries. When the offset within the segment grows 
  35. past hex FFFF (ie. 65535 decimal), it wraps back to zero, and thus you'll end 
  36. up overwriting the beginning of the segment.
  37.  
  38. One way to read large amounts of data to one single array, is to use the huge 
  39. memory model, in which your arrays can span multiple segments. You need to use 
  40. a pointer (double huge *ptr) and huge alloc space for it. When you read the 
  41. file, you must still do it in pieces that won't cross segment boundaries.
  42.  
  43. This holds for Windows 3.x as well, because it too is a 16-bit product and 
  44. uses segments (although there you'd rather use GlobalAlloc etc).
  45.  
  46. HTH,
  47. AriL
  48.  
  49. -- 
  50. All my opinions are mine and mine alone.
  51.